﻿//-----------------------------------------------------------------------
// <copyright file="BidderRespositoryTests.cs" company="Transilvania University of Brasov">
//     Copyright (c) Nagy Robert. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace TestDataAccessLayer
{
    using System;
    using System.Collections.Generic;

    using DataAccessLayer.Auctioneer;
    using DomainModel;
    using Helpers;
    using NUnit.Framework;

    /// <summary>
    /// Tests the BidderRepository class
    /// </summary>
    [TestFixture]
    public class BidderRespositoryTests
    {
        /// <summary>
        /// The repository
        /// </summary>
        private IBidderRepository repository = new BidderRepository();

        /// <summary>
        /// Tests the invalid entity should not be added.
        /// </summary>
        [Test]
        public void TestInvalidEntityShouldNotBeAdded()
        {
            Assert.Throws<ArgumentException>(() => this.repository.Add(new DTOBidder()));
        }

        /// <summary>
        /// Tests the valid entity should be added.
        /// </summary>
        [Test, Rollback]
        public void TestValidEntityShouldBeAdded()
        {
            DTOBidder dto = new DTOBidder
            {
                FirstName = "FirstName",
                LastName = "LastName",
                Password = "Password",
                Username = "Username"
            };

            this.repository.Add(dto);
            DTOBidder foundAuctioneer = this.repository.Find(dto.Id);
        }

        /// <summary>
        /// Tests the invalid entity should not be edited.
        /// </summary>
        [Test]
        public void TestInvalidEntityShouldNotBeEdited()
        {
            Assert.Throws<ArgumentException>(() => this.repository.Edit(new DTOBidder()));
        }

        /// <summary>
        /// Tests the valid entity should be edited.
        /// </summary>
        [Test, Rollback]
        public void TestValidEntityShouldBeEdited()
        {
            DTOBidder dto = BidderTestHelper.CreateDatabaseEntry();

            dto.FirstName = "NewFirstName";
            dto.LastName = "NewLastName";
            dto.Password = "NewPassword";
            dto.Username = "NewUsername";

            this.repository.Edit(dto);

            DTOBidder result = this.repository.Find(dto.Id);

            Assert.NotNull(result);
            Assert.AreEqual(dto.FirstName, result.FirstName);
            Assert.AreEqual(dto.Id, result.Id);
            Assert.AreEqual(dto.LastName, result.LastName);
            Assert.AreEqual(dto.Password, result.Password);
            Assert.AreEqual(dto.Username, result.Username);
        }

        /// <summary>
        /// Tests the invalid identifier should not delete.
        /// </summary>
        /// <param name="id">The identifier.</param>
        [Test]
        public void TestInvalidIdShouldNotDelete([Values(-1, 0)] int id)
        {
            Assert.Throws<ArgumentException>(() => this.repository.Delete(id));
        }

        /// <summary>
        /// Tests the valid identifier should delete.
        /// </summary>
        [Test, Rollback]
        public void TestValidIdShouldDelete()
        {
            DTOBidder dto = BidderTestHelper.CreateDatabaseEntry();
            this.repository.Delete(dto.Id);

            DTOBidder foundAuctioneer = this.repository.Find(dto.Id);
            Assert.IsNull(foundAuctioneer);
        }

        /// <summary>
        /// Tests the get all.
        /// </summary>
        [Test, Rollback]
        public void TestGetAll()
        {
            DTOBidder dto = BidderTestHelper.CreateDatabaseEntry();
            List<DTOBidder> resultList = this.repository.GetAll();
            Assert.IsTrue(resultList.Exists(a => a.FirstName.Equals(dto.FirstName) &&
                                        a.LastName.Equals(dto.LastName) &&
                                        a.Password.Equals(dto.Password) &&
                                        a.Username.Equals(dto.Username)));
        }
    }
}
